package test

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
    "github.com/flipped-aurora/gin-vue-admin/server/model/test"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    testReq "github.com/flipped-aurora/gin-vue-admin/server/model/test/request"
    "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
    "github.com/flipped-aurora/gin-vue-admin/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
)

type StaffApi struct {
}

var staffService = service.ServiceGroupApp.TestServiceGroup.StaffService


// CreateStaff 创建staff表
// @Tags Staff
// @Summary 创建staff表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body test.Staff true "创建staff表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /staff/createStaff [post]
func (staffApi *StaffApi) CreateStaff(c *gin.Context) {
	var staff test.Staff
	err := c.ShouldBindJSON(&staff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := staffService.CreateStaff(&staff); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteStaff 删除staff表
// @Tags Staff
// @Summary 删除staff表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body test.Staff true "删除staff表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /staff/deleteStaff [delete]
func (staffApi *StaffApi) DeleteStaff(c *gin.Context) {
	var staff test.Staff
	err := c.ShouldBindJSON(&staff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := staffService.DeleteStaff(staff); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteStaffByIds 批量删除staff表
// @Tags Staff
// @Summary 批量删除staff表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除staff表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /staff/deleteStaffByIds [delete]
func (staffApi *StaffApi) DeleteStaffByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := staffService.DeleteStaffByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateStaff 更新staff表
// @Tags Staff
// @Summary 更新staff表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body test.Staff true "更新staff表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /staff/updateStaff [put]
func (staffApi *StaffApi) UpdateStaff(c *gin.Context) {
	var staff test.Staff
	err := c.ShouldBindJSON(&staff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := staffService.UpdateStaff(staff); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindStaff 用id查询staff表
// @Tags Staff
// @Summary 用id查询staff表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query test.Staff true "用id查询staff表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /staff/findStaff [get]
func (staffApi *StaffApi) FindStaff(c *gin.Context) {
	var staff test.Staff
	err := c.ShouldBindQuery(&staff)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if restaff, err := staffService.GetStaff(staff.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"restaff": restaff}, c)
	}
}

// GetStaffList 分页获取staff表列表
// @Tags Staff
// @Summary 分页获取staff表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query testReq.StaffSearch true "分页获取staff表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /staff/getStaffList [get]
func (staffApi *StaffApi) GetStaffList(c *gin.Context) {
	var pageInfo testReq.StaffSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := staffService.GetStaffInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}
